//
//  XPViewController.m
//  8-1(下) 核心动画
//
//  Created by mac on 15-8-17.
//  Copyright (c) 2015年 zhiyou. All rights reserved.
//
/*
 核心动画的顶级父类CAAnimation,是一个抽象类,不能创建对象使用.
 
 CAPropertyAnimation也是抽象的子类,它支持动画显示图层关键路径中定制的属性.
 
 1,CABasicAnimation 最基础的动画类,简单的为图层的属性提供修改.
 
 2,CAAnimationGroup 动画组
 
 3,CAKeyframeAnimation 关键帧动画
 
 4,CATransaction 事务动画
 
 5,CATransition 过度动画
 
 动画 : 隐式动画   显式动画
 
 */
#import "XPViewController.h"

@interface XPViewController ()
{

    CALayer *layer;
    CALayer *layer1;
}
@end

@implementation XPViewController

- (void)viewDidLoad
{
    
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    layer=[CALayer layer];
   layer.backgroundColor=[UIColor redColor].CGColor;
    layer.frame=CGRectMake(0, 0, 200, 200);
    layer.cornerRadius=100;
    layer.masksToBounds=YES;
     [self.view.layer addSublayer:layer];
    layer1 =[CALayer layer];
    layer1.frame=CGRectMake(0, 0, 200, 200);
    layer1.backgroundColor=[UIColor blueColor].CGColor;
    
    layer1.cornerRadius=100;
    layer1.opacity=1;
    layer1.masksToBounds=YES;

    [layer addSublayer:layer1];
   
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    
    button.frame=CGRectMake(110, 250, 100, 50);
    button.backgroundColor=[UIColor blueColor];
    [button setTitle:@"开始动画" forState:0];
    [button addTarget:self action:@selector(beginAnimation) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}
-(void)beginAnimation
{
    //NSLog(@"开始动画");
    //CATransaction 事务动画 隐式操作
    [CATransaction begin];
    [CATransaction setAnimationDuration:1.0];
    //取消动画效果
    //[CATransaction setDisableActions:YES];
    layer.opacity=0.3;
    //可以嵌套使用,但是等外层的commit 之后动画才会发生.
 
    [CATransaction commit];
    //[self beginBasiAni];
   // [self beginKeyFrameAni];
    [self beginTrs];
}
-(void)beginBasiAni
{
    //创建基础动画对象
    //animationWithKeyPath:告诉动画对象我要发生什么属性变化  KVC 键值编码

    CABasicAnimation *posAni=[CABasicAnimation animationWithKeyPath:@"position"];
    [posAni setDuration:2.0];
    //返回动画
    posAni.autoreverses=YES;
    //fileMode 决定当前对象过了活动时间段的行为
    //
//    posAni.removedOnCompletion=NO;
//    posAni.fillMode=kCAFillModeForwards;
    
    // 设值要移动的位置.
   
    posAni.fromValue=[NSValue valueWithCGPoint:layer.position];
    CGPoint toPoint=layer.position;
    toPoint.y +=150;
    posAni.toValue=[NSValue valueWithCGPoint:toPoint];
    //[layer addAnimation:posAni forKey:nil];
    //旋转动画
    CABasicAnimation *rotaAni=[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    rotaAni.duration=2.0;
    rotaAni.byValue=[NSNumber numberWithFloat:M_PI/2];
    //[layer addAnimation:rotaAni forKey:nil];
    //扩大
    CABasicAnimation *scalAni=[CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scalAni.duration=2.0;
    scalAni.toValue=[NSNumber numberWithFloat:2.0];
   // [layer addAnimation:scalAni forKey:nil];
    CAAnimationGroup *group=[CAAnimationGroup animation];
    group.animations=[NSArray arrayWithObjects:posAni,rotaAni,scalAni, nil];
    group.duration=2.0;
   // [layer addAnimation:group forKey:nil];

   

}
-(void)beginKeyFrameAni{
    CAKeyframeAnimation *keyAni=[CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
    keyAni.values=[NSArray arrayWithObjects:(id)layer.backgroundColor,(id)[UIColor redColor].CGColor, (id)[UIColor whiteColor].CGColor, (id)[UIColor blueColor].CGColor, nil];
    keyAni.duration=3.0;
   //[layer addAnimation:keyAni forKey:nil];
    
CAKeyframeAnimation *posAni=[CAKeyframeAnimation animationWithKeyPath:@"position"];
    posAni.duration=3.0;
    posAni.values=[NSArray arrayWithObjects:[NSValue valueWithCGPoint:layer.position],[NSValue valueWithCGPoint:CGPointMake(200, 150)],[NSValue valueWithCGPoint:CGPointMake(200, 200)],[NSValue valueWithCGPoint:CGPointMake(200, 350)], nil];
    posAni.keyTimes=[NSArray arrayWithObjects:[NSNumber numberWithFloat:0],[NSNumber numberWithFloat:0.2],[NSNumber numberWithFloat:0.8],[NSNumber numberWithFloat:1], nil];
    [layer addAnimation:posAni forKey:nil];
}
-(void)beginTrs{

    CATransition * ani=[CATransition animation];
    ani.duration=1.8;
    ani.type=@"oglFlip";
   
    //layer1.contents=(id)[UIImage imageNamed:@"1.jpg"].CGImage;
      UIImage *image=[UIImage imageNamed:@"1.jpg"];
    [layer1 setContents:(id)image.CGImage];

    [layer
     addAnimation:ani forKey:nil];
    
}
-(void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
